package com.lyw.utils;

import java.awt.*;

/**
 * 颜色辅助类
 */
public class ColorUtil {

  // 定义七种颜色的常量
  private static final int BLACK = 0;
  private static final int WHITE = 1;
  private static final int GREEN = 2;
  private static final int BLUE = 3;
  private static final int RED = 4;
  private static final int YELLOW = 5;
  private static final int ORANGE = 6;



  private static final int[] COLORS = {
          0x000000, // 黑色
          0xFFFFFF, // 白色
          0x00FF00, // 绿色
          0x0000FF, // 蓝色
          0xFF0000, // 红色
          0xFFFF00, // 黄色
          0xFFA500  // 橙色
  };

  public static int rgb2HsvGetValue(int rgb) {
    return convertToSevenColor((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff);
  }

  public static int rgb2HsvGetValue(int[] colors) {

    return rgb2HsvGetValue(colors[0], colors[1], colors[2]);
  }

  public static int rgb2HsvGetValue(int r, int b, int g) {
    float[] hsv = Color.RGBtoHSB(r, g, b, null);
    if (hsv[1] < 0.2) {
      if (hsv[2] > 0.5) {
        return -1;
      }else {
        return 0;
      }
    }
    if (hsv[2] < 0.3) {
      return 0;
    }
    if (hsv[0] > 0.03 && hsv[0] < 0.35) {
      System.out.println("红");
    } else if (hsv[0] > 0.35 && hsv[0] < 0.65) {
      System.out.println("绿");
    } else if (hsv[0] > 0.65 && hsv[0] < 0.97) {
      System.out.println("蓝");
    } else if (hsv[1] > 0.3 && hsv[2] > 0.4) {
      System.out.println("紫");
    } else if (hsv[1] > 0.4 && hsv[2] > 0.5) {
      System.out.println("靛");
    } else if (hsv[1] > 0.4 && hsv[2] > 0.3) {
      System.out.println("蓝绿");
    } else if (hsv[1] > 0.3 && hsv[2] > 0.2) {
      System.out.println("黄");
    } else {
      System.out.println("橙");
    }

    return 2;
    /*float v = Color.RGBtoHSB(r, g, b, null)[0];
    int res = 0;
    if (v == 0) {
      return res;
    } else if (v > 0 && v < 0.16666666666666666) {
      res = 1;
    } else if (v < 0.3333333333333333) {
      res = 2;
    } else if (v < 0.5) {
      res = 3;
    } else if (v < 0.6666666666666666) {
      res = 4;
    } else if (v < 0.8333333333333334) {
      res = 5;
    } else if (v < 0.9999999999999999) {
      res = 6;
    } else {
      res = 7;
    }
    return res;*/
  }


  public static String getColorName(int r, int b, int g) {

    String colorName = "";
    float[] hsv = Color.RGBtoHSB(r, g, b, null);
    if (hsv[0] == 0) {
      colorName = "黑/白";
    } else if (hsv[0] <= 0.1667) {
      colorName = "紫";
    } else if (hsv[0] < 0.3333333333333333) {
      colorName = "靛";
    } else if (hsv[0] < 0.5) {
      colorName = "蓝";
    } else if (hsv[0] < 0.6666666666666666) {
      colorName = "绿";
    } else if (hsv[0] < 0.8333333333333334) {
      colorName = "黄";
    } else if (hsv[0] < 0.9999999999999999) {
      colorName = "橙";
    } else {
      colorName = "红";
    }
    return colorName;
  }

  // 方法：将RGB颜色值转换为七色图数据
  public static int convertToSevenColor(int r, int b, int g) {
    float[] hsv = Color.RGBtoHSB(r, g, b, null);

    // 取出色调 (H)
    float hue = hsv[0];
    float saturation = hsv[1];
    float value = hsv[2];

    if (saturation < 0.2) {
      if (value > 0.5) {
        return WHITE;
      }else {
        return BLACK;
      }
    }
    if (value < 0.3) {
      return BLACK;
    }
    if (hue < 0.04) {
      return RED;
    }else if (hue < 0.12) {
      return ORANGE;
    }else if (hue < 0.18) {
      return YELLOW;
    }else if (hue < 0.5){  //0.43
      return GREEN;
    }else if (hue < 0.7){
      return BLUE;
    }else {
      return RED;
    }
  }
}
